//
//  ITUserSettingInputTextView.m
//  ITBE
//
//  Created by ITBE on 2019/2/28.
//  Copyright © 2019 ITBE. All rights reserved.
//

#import "ITUserSettingInputTextView.h"

@implementation ITUserSettingInputTextView
-(instancetype)initWithTitle:(NSString *)title placeHolder:(NSString *)placeHolder maxLength:(int)maxLength frame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //[self setBackgroundColor:[UIColor colorWithHexString:@"#2E459E"]];
        [self gradientColorWithStartPoint:CGPointMake(0, 0) toPoint:CGPointMake(0, 1) fromColor:ColorWithHex(0x1F2538) toColor:ColorWithHex(0x16161A) locations:nil];
        _titleLabel = [[UILabel alloc] init];
        [_titleLabel setTextColor:[UIColor colorWithHexString:@"#FFFFFf"]];
        [_titleLabel setFont:[UIFont systemFontOfSize:16]];
        [_titleLabel setText:title];
        [_titleLabel setTextAlignment:NSTextAlignmentCenter];
        [self addSubview:_titleLabel];
        
        _textField = [FSTextView textView];
        [_textField setBackgroundColor:ColorWithHexAplha(0x000000, 0.14)];
        [_textField setTextColor:[UIColor colorWithHexString:@"#FFFFFf"]];
        [_textField setPlaceholder:placeHolder];
        [_textField setCornerRadius:4];
        _textField.keyboardAppearance = UIKeyboardAppearanceDark;
        [_textField setPlaceholderColor:[UIColor colorWithHexString:@"#FFFFFf"]];
        [_textField setFont:[UIFont systemFontOfSize:16]];
        [_textField setMaxLength:maxLength];
        @weakify(self);
        [_textField addTextDidChangeHandler:^(FSTextView *textView) {
            [weak_self updateTextNumber];
        }];
        [self addSubview:_textField];
        
        _notifyLabel = [[UILabel alloc] init];
        //[_notifyLabel setText:[NSString stringWithFormat:[AppUtils titleForKey:@"%d words limited"],maxLength]];
        [_notifyLabel setTextColor:[UIColor colorWithHexString:@"#FFFFFf"]];
        [_notifyLabel setFont:[UIFont systemFontOfSize:13]];
        [_notifyLabel setTextAlignment:NSTextAlignmentLeft];
        [self addSubview:_notifyLabel];
        [self updateTextNumber];
        
        _completeButton = [[UIButton alloc] init];
        [_completeButton setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
        [_completeButton setBackgroundColor:[UIColor colorWithHexString:@"#4A90E2"]];
        [_completeButton.layer setCornerRadius:14];
        [_completeButton.layer setMasksToBounds:YES];
        [_completeButton setTitle:[AppUtils titleForKey:@"comfirm"] forState:UIControlStateNormal];
        [_completeButton.titleLabel setFont:[UIFont systemFontOfSize:12]];
        [_completeButton addTarget:self action:@selector(clickComplete) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_completeButton];
        
        _cancelButton = [[UIButton alloc] init];
        [_cancelButton setTitleColor:[UIColor colorWithHexString:@"#_textField"] forState:UIControlStateNormal];
        [_cancelButton setBackgroundColor:_textField.backgroundColor];
        [_cancelButton.layer setCornerRadius:14];
        [_cancelButton.layer setMasksToBounds:YES];
        [_cancelButton.layer setBorderColor:[UIColor colorWithHexString:@"#979797"].CGColor];
        [_cancelButton.layer setBorderWidth:0.5];
        [_cancelButton setTitle:[AppUtils titleForKey:@"cancel"] forState:UIControlStateNormal];
        [_cancelButton.titleLabel setFont:[UIFont systemFontOfSize:12]];
        [_cancelButton addTarget:self action:@selector(clickCancel) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_cancelButton];
        
        [self makeConstraints];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

- (void)updateTextNumber{
    long max = self->_textField.maxLength;
    NSString *text = [AppUtils titleForKey:@"%ld words limited %lu/%ld",max,(unsigned long)_textField.text.length,max];
    [self->_notifyLabel setText:text];
}

-(void)makeConstraints
{
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(9);
        make.height.equalTo(@(22));
        make.centerX.equalTo(self);
    }];
    
    [_completeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self).offset(-21);
        make.bottom.equalTo(self).offset(-16);
        make.width.equalTo(@(72));
        make.height.equalTo(@(28));
    }];
    
    [_cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self->_completeButton.mas_leading).offset(-7);
        make.width.equalTo(@(72));
        make.height.equalTo(@(28));
        make.centerY.equalTo(self->_completeButton.mas_centerY);
    }];
    
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_titleLabel.mas_bottom).offset(7.5);
        make.leading.equalTo(self).offset(17.5);
        make.trailing.equalTo(self).offset(-17.5);
        make.bottom.equalTo(self->_completeButton.mas_top).offset(-7);
    }];
    
    [_notifyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self->_completeButton);
        make.leading.equalTo(self).offset(22.5);
        make.height.equalTo(@(18));
    }];
}

-(void)becomeFirstResponseWithText:(NSString *)text
{
    if (![AppUtils isNullStr:text]) {
        [_textField setText:text];
    }
    if (![_textField isFirstResponder]) {
        [_textField becomeFirstResponder];
    }
}

-(void)clickComplete
{
    [_textField resignFirstResponder];
    if ([self.delegate respondsToSelector:@selector(resignFirstResponseWithText:)]) {
        [self.delegate resignFirstResponseWithText:_textField.text];
    }
}

-(void)clickCancel
{
    [_textField resignFirstResponder];
    if ([self.delegate respondsToSelector:@selector(didCancelAddComment)]) {
        [self.delegate didCancelAddComment];
    }
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    
    return YES;
}



- (void)textViewDidEndEditing:(UITextView *)textView
{
    [self clickComplete];
}

- (void)keyBoardWillShow:(NSNotification *) note {
    // 获取用户信息
    NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:note.userInfo];
    // 获取键盘高度
    CGRect keyBoardBounds  = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat keyBoardHeight = keyBoardBounds.size.height;
    // 获取键盘动画时间
    CGFloat animationTime  = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    // 定义好动作
    void (^animation)(void) = ^void(void) {
        self.transform = CGAffineTransformMakeTranslation(0, -keyBoardHeight - self.frame.size.height);
    };
    
    if (animationTime > 0) {
        [UIView animateWithDuration:animationTime animations:animation];
    } else {
        animation();
    }
    
}

- (void)keyBoardWillHide:(NSNotification *) note {
    // 获取用户信息
    NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:note.userInfo];
    // 获取键盘动画时间
    CGFloat animationTime  = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    // 定义好动作
    void (^animation)(void) = ^void(void) {
        self.transform = CGAffineTransformIdentity;
    };
    
    if (animationTime > 0) {
        [UIView animateWithDuration:animationTime animations:animation];
    } else {
        animation();
    }
}

-(void)removeFromSuperview
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super removeFromSuperview];
}
@end
